﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using Common.Entities;
using MobileSales.Interface;
using MobileSales.Presenter;


namespace MobileSales
{
    public partial class SheduleVisitsForm : Form, ISheduleVisitsView
    {
        #region IGraphicsVisitView Members

        public void SetBindingSource(BindingSource bindingSource)
        {
            grapVisDataGrid.DataSource = bindingSource;
        }

        public bool IsChange
        {
            get { return isChange; }
            set { isChange = value; }
        }

        public void SetSelectFilter(Filter[] f)
        {
            foreach (Filter filter in f)
            {
                filterComboBox.Items.Add(filter);
            }
        }

        public Filter FilterValue
        {
            get
            {
                return (Filter)filterComboBox.SelectedItem;
            }
        }

        public string ActiveColumn
        {
            get
            {
                return
                    grapVisDataGrid.TableStyles[0].GridColumnStyles[grapVisDataGrid.CurrentCell.ColumnNumber].
                        MappingName;
                 }
        }

        public void CloseAddDialog()
        {
            addBuyPointPanel.Visible = false;
        }

        public void ShowAddVisitDialog(AddVisitDialog visitDialog)
        {
            visitDialog.Dock = DockStyle.Fill;
            addBuyPointPanel.Controls.Add(visitDialog);
            addBuyPointPanel.Visible = true;
        }

        public event EventHandler<EventArgs> SetFilter;

        public event EventHandler<EventArgs> EditRecord;

        public event EventHandler<EventArgs> DeleteRecord;

        public event EventHandler<EventArgs> AddRecord;

        public event EventHandler<EventArgs> SaveChanges;

        #endregion

        private bool isChange = false;

        public SheduleVisitsForm()
        {
            InitializeComponent();
           
            DataGridTableStyle ts = new DataGridTableStyle();
            DataGridColumnStyle dataColumn;

            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "П";
            dataColumn.MappingName = "Monday";
            dataColumn.NullText = "null";
            dataColumn.Width = 17;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "В";
            dataColumn.MappingName = "Tuesday";
            dataColumn.NullText = "null";
            dataColumn.Width = 17;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "С";
            dataColumn.MappingName = "Wednesday";
            dataColumn.NullText = "null";
            dataColumn.Width = 17;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "Ч";
            dataColumn.MappingName = "Thursday";
            dataColumn.NullText = "null";
            dataColumn.Width = 17;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "П";
            dataColumn.MappingName = "Friday";
            dataColumn.NullText = "null";
            dataColumn.Width = 17;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "С";
            dataColumn.MappingName = "Saturday";
            dataColumn.NullText = "null";
            dataColumn.Width = 17;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridCheckBoxColumn();
            dataColumn.HeaderText = "Н";
            dataColumn.MappingName = "Sunday";
            dataColumn.NullText = "null";
            dataColumn.Width = 17;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Назва торгової точки";
            dataColumn.MappingName = "NameBuyPoint";
            dataColumn.NullText = "";
            dataColumn.Width = 150;
            ts.GridColumnStyles.Add(dataColumn);

            dataColumn = new DataGridTextBoxColumn();
            dataColumn.HeaderText = "Адреса";
            dataColumn.MappingName = "Adress";
            dataColumn.NullText = "";
            dataColumn.Width = 200;
            ts.GridColumnStyles.Add(dataColumn);

            ts.MappingName = "SheduleVisits";
            grapVisDataGrid.TableStyles.Add(ts);

            new SheduleVisitsPresenter(this);
            grapVisDataGrid.RowHeadersVisible = false;
            filterComboBox.SelectedIndex = 0;
        }

        private void grapVisDataGrid_Click(object sender, EventArgs e)
        {
            if (grapVisDataGrid.TableStyles[0].GridColumnStyles[grapVisDataGrid.CurrentCell.ColumnNumber] is DataGridCheckBoxColumn)
                EditRecord(null, null);
        }

        private void addMenuItem_Click(object sender, EventArgs e)
        {
            AddRecord(null, null);
        }

        private void deleteMenuItem_Click(object sender, EventArgs e)
        {
            DeleteRecord(null, null);
        }

        private void GrahicsVisits_Deactivate(object sender, EventArgs e)
        {
            if (IsChange)
            {
                if (DialogResult.Yes == MessageBox.Show("Дані змінилися ви бажаєте зберегти зміни ?", "Увага!",
                                           MessageBoxButtons.YesNo,
                                           MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                {
                    SaveChanges(null, null);
                }
            }
        }

        private void filterComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetFilter(null, null);
        }
    }
}